package com.jzy.gulimall.order.listener;

import com.jzy.gulimall.order.entity.OrderEntity;
import com.jzy.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author jzy
 * @date 2020/9/19 17:48
 */
@Component
@RabbitListener(queues="order.release.order.queue")
public class OrderCloseListener {

    @Resource
    private OrderService orderService;

    /**
     * 订单过期 关闭订单
     */
    @RabbitHandler
    public void handlerOrderRelease(Message message, OrderEntity orderEntity, Channel channel) throws IOException {
        //byte[] body = message.getBody();
        //MessageProperties properties = message.getMessageProperties();
        //channel内自增
        System.out.println("收到过期的订单信息,准备关闭订单"+orderEntity.getOrderSn());
        try {
            orderService.closeOrder(orderEntity);
            //手动调用支付宝收单,支付失败
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
